<template>
  <div class="article">
    <el-card class="margin-30">
      <div slot="header" class="clearfix">
        <span>条件查询</span>
      </div>
      <el-form ref="form" :model="form" label-width="80px" size="small">
        <el-row :gutter="20">
          <el-col :span="6" :offset="0">
            <el-form-item label="作者">
              <el-input v-model="form.author" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="0">
            <el-form-item label="标题">
              <el-input v-model="form.title" placeholder="标题" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="0">
            <el-form-item label="编辑类型">
              <!-- editorType (integer, optional): 编辑器类型: 0 富文本; 1 markdown , -->
              <el-select v-model="form.editorType" style="width:100%" placeholder="请选择">
                <el-option
                  label="富文本"
                  :value="0"
                />
                <el-option
                  label="markdown"
                  :value="1"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6" :offset="18">
            <el-form-item>
              <el-button type="default" size="mini" @click="resetForm">重置</el-button>
              <el-button type="primary" size="mini" @click="doSearchArticle">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
    </el-card>

    <!-- 文章列表 -->
    <el-card shadow="never" class="margin-30">
      <div slot="header">
        <el-button type="primary" size="mini" @click="goAddArticle">新增</el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="artilceList"
        border
        stripe
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >

        <el-table-column
          label="#"
          align="center"
          width="50"
          type="index"
        />
        <el-table-column
          label="文章标题"
          prop="title"
          align="center"
        />
        <el-table-column
          label="图片展示"
          width="120"
          align="center"
        >
          <template slot-scope="scope">
            <img width="80" height="80" :src="scope.row.coverImg" alt="">
          </template>
        </el-table-column>
        <el-table-column
          label="添加时间"
          prop="createTime"
          align="center"
        />
        <el-table-column
          label="文章作者"
          prop="author"
          align="center"
        />
        <el-table-column
          label="文章是否展示"
          prop="isShow"
          align="center"
        >
          <template slot-scope="scope">
            <!-- isShow (integer, optional): 是否显示: 0 否; 1 是 , -->
            <el-switch v-model="scope.row.isShow" :active-value="1" :inactive-value="0" @change="articleIsShowChange(scope.row.id,scope.row.isShow)" />
          </template>
        </el-table-column>
        <el-table-column
          label="编辑类型"
          prop="editorType"
          align="center"
        >
          <template slot-scope="scope">
            <!-- editorType (integer, optional): 编辑器类型: 0 富文本; 1 markdown , -->
            <el-button v-if="scope.row.editorType===0" type="primary" size="mini">富文本</el-button>
            <el-button v-if="scope.row.editorType===1" type="warning" size="mini">markdown</el-button>

          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
        >
          <template slot-scope="scope">
            <el-button type="text" size="mini" icon="el-icon-view" @click="goEditArticle(scope.row.id)">编辑文章</el-button>
            <el-button type="text" size="mini" style="color:red" @click="deleteArticleById(scope.row.id)">删除</el-button>

          </template>
        </el-table-column>

      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="text-align:left;margin-top:20px;"
        :current-page="paginationObj.start"
        :page-sizes="paginationObj.pageSizes"
        :page-size="paginationObj.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="paginationObj.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

  </div>
</template>
<script>
import { findArticles as findArticlesApi,
  changeShowStatus as changeShowStatusApi,
  delArticle as delArticleApi
} from '@/api/article/index'
import mix from '@/mixins/index'
import { filterNullParams } from '@/utils'
export default {
  name: 'ArticleList',
  mixins: [mix],
  data() {
    return {
      form: {},
      loading: true,
      artilceList: []
    }
  },
  created() {
    this.getArticleAList()
  },
  mounted() {
    // console.log('this=>', this)
    // this.sum(1, 2)
  },
  methods: {
    // 去添加文章
    goAddArticle() {
      this.$router.push({ name: 'ArticleDetail' })
    },
    // 搜索
    doSearchArticle() {
      // 重置页码
      this.paginationObj.start = 1
      this.getArticleAList()
    },
    // 获取文章列表
    async getArticleAList() {
      // 开启进度条
      this.loading = true
      const newForm = filterNullParams(this.form)
      const res = await findArticlesApi(this.paginationObj.start, this.paginationObj.limit, newForm)
      // console.log(res)
      const { success, data, message } = res
      if (success) {
        this.artilceList = data.rows
        this.paginationObj.total = data.total
        // 关闭进度条
        this.loading = false
      } else {
        this.$message.error(message)
      }
    },

    // 修改文章显示状态
    async articleIsShowChange(id, status) {
      // console.log(id, status)
      const res = await changeShowStatusApi({ id, isShow: status })
      const { success, message } = res
      if (success) {
        this.$message.success('文章状态修改成功')
      } else {
        this.$message.error(message)
      }
    },
    // 去编辑文章
    goEditArticle(id) {
      this.$router.push({ name: 'ArticleDetail', query: { id }})
    },
    // 根据 id 删除文章
    deleteArticleById(id) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 确定
        const res = await delArticleApi(id)
        const { success, message } = res
        if (success) {
          this.$message.success('文章删除成功')
          // 重新获取数据
          this.getArticleAList()
        } else {
          this.$message.error(message)
        }
      }).catch(() => {
        // 取消
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 每页数据修改
    handleSizeChange(val) {
      // 开启进度条
      this.loading = true
      // console.log(`每页 ${val} 条`)
      // 重置页码
      this.paginationObj.start = 1
      // 重置 limit
      this.paginationObj.limit = val
      this.getArticleAList()
    },
    // 当前页码修改
    handleCurrentChange(val) {
      // 开启进度条
      this.loading = true
      // console.log(`当前页: ${val}`)
      // 重置页码
      this.paginationObj.start = val
      this.getArticleAList()
    },
    // 重置表单
    resetForm() {
      // this.$refs.form.resetFields()
      this.form = {}
      this.getArticleAList()
    }
  }
}
</script>
<style lang="scss" scoped>
.article{
  ::v-deep .el-form-item label{
    font-weight: normal;
  }
}
</style>
